package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.TaskNodeTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserTaskManageService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task.EventExceptionHandler;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task.UserInfoHandleProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.TaskCompletedKeyEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.eventconfig.EventExceptionDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.FlowTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.*;

/**
　　* @description: 会签任务节点完成之后同步数据处理器
　　* @author shenrm
　　* @date 2021/12/29 10:27
　　*/
@Slf4j
@Service
@Order(2)
public class MultiExecutionCompletedSyncDataProcessor implements MultiExecutionCompletedProcessor{

    @Autowired
    private UserTaskManageService userTaskManageService;

    @Autowired
    private UserService userService;

    @Autowired
    private FlowTaskTraceRepository flowTaskTraceRepository;

    @Autowired
    private UserInfoHandleProcessor userInfoHandleProcessor;

    @Autowired
    private EventExceptionHandler eventExceptionHandler;

    @Override
    public void process(ExecutionEntity executionEntity) throws Exception {
        log.info("MultiExecutionCompletedSyncDataProcessor同步待办组件接口入参：{}",executionEntity);
        // 获取该流程实例中该任务节点的待办处理记录
        List<FlowTaskTraceVo> flowTaskTraceVos = flowTaskTraceRepository.getFlowTaskTracesByProInsIdAndNode(executionEntity.getProcessInstanceId(),executionEntity.getCurrentActivityId());
        if(CollectionUtils.isEmpty(flowTaskTraceVos)) {
            return;
        }

        // 该任务节点所有的待办任务,与待办组件约定,给全量
        List<String> taskInstanceIds = new ArrayList<>();
        for (FlowTaskTraceVo vo : flowTaskTraceVos) {
            //修复并列的子流程状态被更新的bug
            if(executionEntity.getId().equals(vo.getParentId())) {
                taskInstanceIds.add(vo.getTaskId());
            }
        }

    }

    /**
     * 回滚待办任务状态
     *
     * @param executionEntity
     * @param taskLinkStatus
     * @param taskNodeType
     * @throws Exception
     */
    @Override
    public void rollbackTask(ExecutionEntity executionEntity, String taskLinkStatus, String taskNodeType) throws Exception {
        // 获取该流程实例中该任务节点的待办处理记录
        log.info("****待办任务回滚接口rollbackTask入参1ExecutionEntity****：{},入参2taskLinkStatus：{},入参3taskNodeType：{}",executionEntity,taskLinkStatus,taskNodeType);
        List<FlowTaskTraceVo> flowTaskTraceVos = flowTaskTraceRepository.getFlowTaskTracesByProInsIdAndNode2(executionEntity.getProcessInstanceId(),executionEntity.getCurrentActivityId(), "complete_time", "desc");
        if(CollectionUtils.isEmpty(flowTaskTraceVos)) {
            return;
        }

    }
}
